<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/3/21
 * Time: 16:30
 */

namespace Admin\Controller;


use Admin\Model\UserListLogic;
use Admin\Model\UserLogic;
use Think\Controller\RestController;

class AdminApiController extends ApiAuthController
{
    /*
     * 获取用户列表
     * 参数
     * gid          可空  group_id
    *  user_name    可空  模糊查询用户名(nickname)
    */
    public function get_user_list()
    {
        $gid = I('gid');
        $nickname = I('user_name');
        //判断是否要查询用户名
        if (!empty($nickname)) {
            $map['a.nickname'] = array('like', '%' . $nickname . '%');
        }
        //判断是否要查询group_id
        if (!empty($gid) && $gid > 0) {
            $map['g.id'] = $gid;
        }
        //新建db视图
        $dbView = D('admin')
            ->alias('a')
            ->join('left join think_auth_group_access as ga on a.id = ga.uid')
            ->join('left join think_auth_group as g on g.id = ga.group_id');
        $result = $dbView
            ->field(array(
                'a.id' => 'id',
                'nickname' => 'name',
                'g.title' => 'department',
                'last_login_time',
                'last_login_ip'
            ))
            ->page(I('get.page'), 15)
            ->where($map)
            ->select();
        $data['users'] = $result;
        //查询最大页数
        $data['max_page'] = ceil(D('admin')
                ->alias('a')
                ->join('left join think_auth_group_access as ga on a.id = ga.uid')
                ->join('left join think_auth_group as g on g.id = ga.group_id')
                ->where($map)->count() / 15);
        \Common\Common\print_json_data(200, $data, '查询成功');
    }

    /**
     * 获取用户信息
     * 参数
     * uid          不可为空 用户id
     */
    public function get_user()
    {
        $uid = I('uid');
        //校验参数
        if (!empty($uid)) {
            $result = D('admin')
                ->field(array(
                    'id',
                    'nickname' => 'name',
                    'last_login_time',
                    'last_login_ip'
                ))
                ->find($uid);
            \Common\Common\print_json_data(200, $result, '查询成功');
            exit;
        }
        \Common\Common\request_parameter_invalid();
    }

    /**
     * 删除用户
     * 参数
     * uid          不可为空 用户id
     */
    public function delete_user()
    {
        $uid = I('uid');
        if (empty($uid)) {
            \Common\Common\request_parameter_invalid();
            exit;
        }
        D('admin')->delete($uid);
        \Common\Common\print_json_data(200, '' . '删除成功');
    }

    /**
     * 获取组列表
     * 参数
     * 无
     */
    public function get_group_list()
    {
        //设置可用状态
        $where['status'] = array('eq', 1);
        $result = D('group')
            ->where($where)
            ->field(array(
                'id',
                'title' => 'group_name'
            ))
            ->select();
        \Common\Common\print_json_data(200, $result, '查询成功');
    }

    /**
     *
     */
    public function get_saler_list()
    {
        $nickname = I('user_name');
        //判断是否要查询用户名
        if (!empty($nickname)) {
            $where['nickname'] = array('like', '%' . $nickname . '%');
        }

        $model = M('wx_users');
        $data['salers'] = $model
            ->where($where)
            ->page(I('page'), 15)
            ->field(array(
                'id',
                'nickname' => 'name',
                'sex',
                'tel',
                'city'
            ))
            ->select();
        $data['max_page'] = ceil($model->where($where)->count() / 15);
        \Common\Common\print_json_data(200, $data, '查询成功');
    }

}